#include "../inc/FileWriter.h"

bool FileWriter::setFileURL(char* FileURL)
{
	bool setFileURLResult = true;
	this->fileURL = fopen(FileURL,"w");
	if(!this->fileURL)
	{
		setFileURLResult=false;
		std::cerr<<"Can not open file: "<<fileURL<<std::endl;
	}
	return setFileURLResult;
}

bool FileWriter::Write(const MatrixData& src) const
{
	bool WriteToFileResult = true;	
	
	if(!fileURL)
	{
		bool WriteToFileResult = false;
		std::cerr<<"Can not write to file."<<std::endl;		
	}
	unsigned int srcSize = src.size();
	if(srcSize > 0)
	{		
	
		unsigned int srcRowSize = src.at(0).size();
		for(unsigned int matrIter = 0;matrIter < srcSize; ++matrIter)
		{
			for(unsigned int rowIter = 0;rowIter < srcRowSize; ++rowIter)
			{
				fprintf(fileURL,"%d",src.at(matrIter).at(rowIter));
				if(rowIter < srcRowSize-1) fprintf(fileURL," ");
			}	

			fprintf(fileURL,"\n");		
		}
	}
return WriteToFileResult;
}

FileWriter::FileWriter(void)
{
	this->fileURL=NULL;
}


FileWriter::~FileWriter(void)
{
	if(this->fileURL)
	{
		fclose (this->fileURL);
	}
}
